package com.thinkfit.jy.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.blankj.utilcode.util.GsonUtils;
import com.common.lib.constant.Constants;
import com.common.lib.constant.EventBusEvent;
import com.common.lib.fragment.BaseFragment;
import com.common.lib.mvp.contract.EmptyContract;
import com.common.lib.mvp.presenter.EmptyPresenter;
import com.common.lib.utils.BaseUtils;
import com.github.mikephil.charting.charts.CombinedChart;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.CombinedData;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.thinkfit.jy.R;
import com.thinkfit.jy.serialport.SerialPortManager;
import com.thinkfit.jy.utils.DeviceTypeUtils;
import com.thinkfit.jy.utils.LogUtils;
import com.thinkfit.jy.utils.MotionDataUtils;
import com.thinkfit.jy.utils.SportsStatusBarViewUtils;
import com.thinkfit.jy.utils.Utils;
import com.thinkfit.jy.view.CCTrackView;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 自由运动信息卡片界面
 */
public class FreeMovementItem0Fragment extends BaseFragment<EmptyContract.Presenter> implements EmptyContract.View {

    int type = 0;
    float num = 0;
    TextView tvCircle,tvTime,tvKcal;
    CCTrackView trackView;
    int distance;
    boolean isFits = true;
    public static FreeMovementItem0Fragment getInstance(int type, float num) {
        Bundle bundle = new Bundle();
        bundle.putInt(Constants.BUNDLE_EXTRA, type);
        bundle.putFloat(Constants.BUNDLE_EXTRA_2, num);
        FreeMovementItem0Fragment fragment = new FreeMovementItem0Fragment();
        fragment.setArguments(bundle);
        return fragment;
    }
    @NonNull
    @Override
    protected EmptyContract.Presenter onCreatePresenter() {
        return new EmptyPresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_free_movement_item_0;
    }


    @Override
    protected void initView(@NonNull View view, @Nullable Bundle savedInstanceState) {
        type = getArguments().getInt(Constants.BUNDLE_EXTRA);
        num = getArguments().getFloat(Constants.BUNDLE_EXTRA_2);
        trackView = view.findViewById(R.id.trackView);
        tvCircle = view.findViewById(R.id.tv_circle);
        tvTime = view.findViewById(R.id.tv_time);
        tvKcal = view.findViewById(R.id.tv_kcal);

    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onReceive(HashMap<String, Object> map) {
        if (map == null) {
            return;
        }

        if (map.containsKey(EventBusEvent.START_RUN)) {
            ArrayList<Integer> target = SportsStatusBarViewUtils.getInstance().getTarget();
            type = Integer.valueOf(target.get(0));
            num = Integer.valueOf(target.get(1));
//            if(type == 0){
//                tvSportsCenter.setText(getString(R.string.app_free_training));
//            }else {
//                tvSportsCenter.setText(SportsStatusBarViewUtils.getInstance().targetType(type, num));
//            }
        }else if (map.containsKey(EventBusEvent.STOP_RUN)){
//            clSportInfo.setVisibility(View.GONE);
//            tvSportsCenter.setText(getString(R.string.app_sports_center));
            num = 0;
            type = 0;
//            stopInitView();
        }else if(map.containsKey(EventBusEvent.PAUSE_RUN)){

        }else if (map.containsKey(EventBusEvent.DEVICE_DATA)) {
            System.out.println("MotionDataUtils -------------> ddddddddddddddddddddddd  CurrentInfo : "+ GsonUtils.toJson((SerialPortManager.CurrentInfo) map.get(EventBusEvent.DEVICE_DATA)));
            SerialPortManager.CurrentInfo currentInfo = (SerialPortManager.CurrentInfo) map.get(EventBusEvent.DEVICE_DATA);
            resetInfo(currentInfo);
        }else if(map.containsKey(EventBusEvent.EQUIPMENT_STATE)){//设备状态
//            SerialPortManager.CurrentInfo currentInfo = (SerialPortManager.CurrentInfo) map.get(EventBusEvent.EQUIPMENT_STATE);
            SerialPortManager.CurrentInfo currentInfo = MotionDataUtils.getInstance().getCurrentInfo();
            resetInfo(currentInfo);
        }else if(map.containsKey(EventBusEvent.SET_TARGET)){
            ArrayList<String> o = (ArrayList<String>) map.get(EventBusEvent.SET_TARGET);
            type = Integer.valueOf(o.get(0));
            num = Integer.valueOf(o.get(1));

        }
    }

    int time;
    public void resetInfo(SerialPortManager.CurrentInfo info) {
        if(time == info.time){
            return;
        }
        time = info.time;
        float kcal = info.kcal /10f;
        int hour = info.time / 3600;
        int minute = (info.time / 60) % 60;
//        int minute = 0;
        int second = info.time % 60;

        float v = (60f / (info.speed/10f))*60;
        int paceHour = (int) (v/3600);
        int paceMin = (int) ((v/ 60) % 60);
        int paceSecond = (int) v % 60;
        if(distance == info.distance){

        }else {
            distance = info.distance;
        }
        if(isFits){
            trackView.setCursorPosition(distance);
            isFits = false;
        }else {
            if(DeviceTypeUtils.getDeviceType() == 2){
                float speedNum = (info.speed*0.1f)/3.6f;
                Float aFloat = Float.valueOf(Utils.getNewFloat(speedNum));
                System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxx  aFloat : "+aFloat);
                if(aFloat > 0){
                    trackView.setSpeedCursorPosition(aFloat);
                }
            }else {
                trackView.setCursorPosition(distance);
            }
        }
        double num = Math.floor(info.distance / 400);
        long round = Math.round(num);
        if(round > 1){
            tvCircle.setText(getString(R.string.app_free_laps,round+""));
        }else {
            tvCircle.setText(getString(R.string.app_free_lap,round+""));
        }
        double min = Math.floor(info.time / 60);
        long time = Math.round(min);
        tvTime.setText(time+"");
        tvKcal.setText(Utils.getNewFloat(kcal));
    }

    @Override
    public void onStop() {
        super.onStop();
        isFits = true;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

        }
    }

}
